//
//  Photo.swift
//  PhotoBrowser
//
//  Created by meng on 16/5/18.
//  Copyright © 2016年 leotao. All rights reserved.
//

import UIKit
import Photos
import Kingfisher
import AssetsLibrary

class Photo {
    var photoURL: NSURL?;
    var image: UIImage?;
    var phAsset: PHAsset?;
    var asset: ALAsset?
    var underlyingImage: UIImage?
    
    init(url: NSURL) {
        self.photoURL = url;
    }
    
    init(image: UIImage) {
        self.image = image;
    }
    
    init(phAsset: PHAsset){
        self.phAsset = phAsset;
    }
    
    init(asset: ALAsset) {
        self.asset = asset;
    }
    
    func loadImageAndNotify() {
        if self.image != nil {
            imageLoadingComplete();
        } else {
            fetchImageAndNotify();
        }
    }
  
    
    func imageLoadingComplete() {
        
    }
    
    func fetchImageAndNotify() {
        if self.photoURL != nil {
            if self.photoURL!.scheme == "asset-library" {   // 本地相册路径
                loadImageWithAssetLibraryURL(self.photoURL!);
            } else if self.photoURL!.isFileReferenceURL() { // 本地文件路径
                loadImageWithFileURL(self.photoURL!);
            } else {    // 普通网络地址
                loadImageWithWebURL(self.photoURL!);
            }
        } else if self.phAsset != nil {
            loadImageWithPhAsset(self.phAsset!, targetSize: CGSizeZero);
        } else if self.asset != nil {
            loadImageWithALAssets(asset!);
        }
    }
    
    func loadImageWithAssetLibraryURL(url: NSURL) {
        
    }
    
    func loadImageWithFileURL(url: NSURL) {
        
    }
    
    func loadImageWithWebURL(url: NSURL) {
        KingfisherManager.sharedManager.retrieveImageWithURL(url, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) in
            let progress = CGFloat(receivedSize) / CGFloat(totalSize);
            
            NSNotificationCenter.defaultCenter().postNotificationName(ProgressChangeNotification, object: self, userInfo: ["progress" : progress]);
            }, completionHandler: { (image, error, cacheType, imageURL) in
                dispatch_async(dispatch_get_main_queue(), {
                    self.image = image;
                    NSNotificationCenter.defaultCenter().postNotificationName(ImageDownloadCompleteNotification, object: self, userInfo: nil);
                });
        });
    }
    
    func loadImageWithPhAsset(asset:PHAsset, targetSize:CGSize) {
        
    }
    
    func loadImageWithALAssets(asset:ALAsset)  {
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 
            let cg_image = asset.defaultRepresentation().fullResolutionImage().takeUnretainedValue();
            self.image = UIImage(CGImage: cg_image);
//            self.image = UIImage(contentsOfFile: asset.defaultRepresentation().url().path!);
            dispatch_async(dispatch_get_main_queue(), {
                NSNotificationCenter.defaultCenter().postNotificationName(ImageDownloadCompleteNotification, object: self, userInfo: nil);
            });
        };
        
    }
//    
//    deinit {
//        print("deinit");
//    }
}
